package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ilioili.proabslist.ListStateListener;
import com.ilioili.proabslist.ProAbsListView;
import com.laoxinwen.app.R;
import com.umeng.fb.FeedbackAgent;

import java.util.ArrayList;

import bean.TabTopicItem;
import bean.wraper.TabTopicListWrapper;
import cn.evun.proabslistview.FreshView;
import cn.evun.proabslistview.LoadView;
import common.BaseActivity;
import common.FastAdapter;
import common.HttpCallBack;
import common.Requester;
import util.DialogUtil;

/**
 * Created by ilioili on 2015/3/17.
 */
public class DigTopicActivity extends BaseActivity implements ListStateListener {

    private final static String INTENT_KEY_ID = "id";
    private final static String ACTION_DIG_NEWS = "dig_news";
    private final static String ACTION_DIG_ARTICLE = "dig_article";
    private final View.OnClickListener listenerForItem = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TabTopicItem news = (TabTopicItem) v.getTag(2 << 24);
            Intent it = TopicActivity.getIntent(DigTopicActivity.this, news.getId());
            startActivity(it);
        }
    };
    private ArrayList<TabTopicItem> dataList = new ArrayList<>();
    private FastAdapter adapter = new FastAdapter() {
        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TabTopicItem tabTopicItem = dataList.get(position);
            Holder holder = null;
            if (null == convertView) {
                convertView = getLayoutInflater().inflate(R.layout.item_topic, parent, false);
                holder = new Holder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.setData(tabTopicItem);
            return convertView;
        }

    };
    private boolean isFirstCreated = true;
    private String itemId;
    @id(R.id.listView)
    private ListView listView;
    private int pageIndex;
    @id(R.id.proAbsListView)
    private ProAbsListView proAbsListView;

    public static Intent getIntentForDigNews(Activity activity, String newsId) {
        Intent it = new Intent(activity, DigTopicActivity.class);
        it.setAction(ACTION_DIG_NEWS);
        it.putExtra(INTENT_KEY_ID, newsId);
        return it;
    }

    public static Intent getIntentForDigArticle(Activity activity, String articleId) {
        Intent it = new Intent(activity, DigTopicActivity.class);
        it.setAction(ACTION_DIG_ARTICLE);
        it.putExtra(INTENT_KEY_ID, articleId);
        return it;
    }

    @Override
    public void onScrollWhileFreshViewVisible(int percentage) {

    }

    @Override
    public void onScrollWhileLoadViewVisible(int percentage) {

    }

    @Override
    public void onStartFreshing() {
        pageIndex = 0;
        requestData();
    }

    @Override
    public void onStartLoading() {
        pageIndex++;
        requestData();
    }

    private void requestData() {
        HttpCallBack callBack = new HttpCallBack<TabTopicListWrapper>() {
            @Override
            public void onSucceed(TabTopicListWrapper newsGroupWraper) {
                if (null != newsGroupWraper.getData() && !newsGroupWraper.getData().isEmpty()) {
                    if (pageIndex == 0) {
                        dataList.clear();
                    }
                    dataList.addAll(newsGroupWraper.getData());
                    if (pageIndex == 0) {
                        listView.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    DialogUtil.getConfirmDialog(DigTopicActivity.this, "提建议给小编", "您给我们提供新闻线索，我们为您深度挖掘！", new DialogUtil.CallBack() {
                        @Override
                        public void onCallBack() {
                            FeedbackAgent agent = new FeedbackAgent(DigTopicActivity.this);
                            agent.startFeedbackActivity();
                        }
                    }).show();
                    //                    new AlertDialog.Builder(DigTopicActivity.this).setTitle("提建议给小编").setNegativeButton("拒绝", null).setPositiveButton("好的",new DialogInterface.OnClickListener() {
                    //                        @Override
                    //                        public void onClick(DialogInterface dialog, int which) {
                    //                            FeedbackAgent agent = new FeedbackAgent(DigTopicActivity.this);
                    //                            agent.startFeedbackActivity();
                    //                        }
                    //                    }).create().show();
                }
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
            }

            @Override
            protected void onNetError() {
                super.onNetError();
                cancelFreshing();
            }

            @Override
            protected void onServerError(TabTopicListWrapper data) {
                super.onServerError(data);
                cancelFreshing();
                //                Toast.makeText(getActivity(), data.getError().getMessage(), Toast.LENGTH_SHORT).show();
            }

            private void cancelFreshing() {
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
            }

        };
        switch (getIntent().getAction()) {
            case ACTION_DIG_ARTICLE:
                Requester.digNewsByArticle(pageIndex, itemId, callBack);
                break;
            case ACTION_DIG_NEWS:
                Requester.digNewsByNews(pageIndex, itemId, callBack);
                break;
        }
    }

    private class Holder {

        private ImageView ivTopicPic;
        private View rootView;
        private TextView tvTopicTitle;

        public Holder(View v) {
            rootView = v;
            ivTopicPic = f(R.id.iv_topic_pic, v);
            tvTopicTitle = f(R.id.tv_topic_title, v);
            v.setOnClickListener(listenerForItem);
            v.setClickable(true);
        }

        public void setData(TabTopicItem tabNewsTopic) {
            rootView.setTag(2 << 24, tabNewsTopic);
            imgPool.bind(getImg(tabNewsTopic.getImg()), ivTopicPic);
            tvTopicTitle.setText(tabNewsTopic.getTitle());
        }
    }

    @Override
    protected void onRightSlide() {
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dig_news_list);
        loadView();
        proAbsListView.setListStateListener(this);
        itemId = getIntent().getStringExtra(INTENT_KEY_ID);
        setListView();
        setFreshView();
        setLoadView();
        if (isFirstCreated) {
            isFirstCreated = false;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    proAbsListView.startFresh();
                }
            }, 250);
        }
    }

    private void setListView() {
        listView = (ListView) proAbsListView.getAbsListView();
        View footView = getLayoutInflater().inflate(R.layout.dig_foot, listView, false);
        f(R.id.bt_dig, footView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                proAbsListView.startLoad();
            }
        });
        listView.addFooterView(footView);
        listView.setAdapter(adapter);
    }

    private void setFreshView() {
        LoadView loadView = new LoadView(this);
        proAbsListView.setLoadView(loadView);
    }

    private void setLoadView() {
        FreshView freshView = new FreshView(this);
        proAbsListView.setFreshView(freshView);
    }
}
